package com.cxc.service;

import com.cxc.bean.Student;
import com.cxc.bean.StudentExample;
import com.cxc.bean.StudentExample.Criteria;
import com.cxc.dao.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;

    //查询所有学生
    public List<Student> getAll() {
        return studentMapper.selectByExampleWithDept(null);
    }

    //校验用户名是否已经存在于数据库
    //true代表当前姓名可用，false代表不可用
    public boolean isExisted(String stuName) {
        StudentExample studentExample = new StudentExample();
        Criteria criteria = studentExample.createCriteria();
        criteria.andStuNameEqualTo(stuName);

        long count = studentMapper.countByExample(studentExample);
        return count == 0;
    }

    //保存学生
    public void addStu(Student student) {
        studentMapper.insertSelective(student);
    }

    //根据id查询学生信息
    public Student getStu(Integer id){
        return studentMapper.selectByPrimaryKey(id);
    }

    //保存更新后的学生信息
    public void updateStu(Student student){
        studentMapper.updateByPrimaryKeySelective(student);
    }

    //单个删除
    public void deleteStuById(Integer id){
        studentMapper.deleteByPrimaryKey(id);
    }

    //批量删除
    public void deleteStuBatch(List<Integer> ids){
        StudentExample example = new StudentExample();
        Criteria criteria = example.createCriteria();
        //delete from xxx where stu_id in(1,2,3)
        criteria.andStuIdIn(ids);
        studentMapper.deleteByExample(example);
    }
}
